﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace DeviceSDK.LED.NuoWa.JT100
{


    public class DesEncrypt
    {
        // Fields
        private static string s_IV = "novamima";
        private static string s_Key = "novamima";

        // Methods
        public static bool Decrypt(byte[] srcData, int validLength, out byte[] desData)
        {
            bool flag;
            byte[] key = new byte[0];
            byte[] iv = new byte[0];
            desData = new byte[0];
            if (!GetIVAndKey(out iv, out key))
            {
                return false;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            try
            {
                using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(key, iv), CryptoStreamMode.Write))
                {
                    stream2.Write(srcData, 0, validLength);
                    stream2.FlushFinalBlock();
                }
                desData = stream.ToArray();
                flag = true;
            }
            catch (Exception)
            {
                desData = null;
                flag = false;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
            return flag;
        }

        public static bool DecryptFile(string srcFile, string destFile)
        {
            if (!File.Exists(srcFile))
            {
                return false;
            }
            if (!Directory.Exists(Path.GetDirectoryName(destFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destFile));
            }
            byte[] key = new byte[0];
            byte[] iv = new byte[0];
            if (!GetIVAndKey(out iv, out key))
            {
                return false;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] buffer = new byte[0x408];
            byte[] buffer4 = null;
            bool flag = false;
            using (FileStream stream = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
            {
                using (FileStream stream2 = new FileStream(destFile, FileMode.Create, FileAccess.Write))
                {
                    int num = 0;
                    int num2 = 0;
                    int count = 0;
                    while (num < stream.Length)
                    {
                        stream.Seek((long)num, SeekOrigin.Begin);
                        count = stream.Read(buffer, 0, buffer.Length);
                        num += count;
                        MemoryStream stream3 = new MemoryStream();
                        try
                        {
                            using (CryptoStream stream4 = new CryptoStream(stream3, provider.CreateDecryptor(key, iv), CryptoStreamMode.Write))
                            {
                                stream4.Write(buffer, 0, count);
                                stream4.FlushFinalBlock();
                            }
                            buffer4 = stream3.ToArray();
                            stream2.Seek((long)num2, SeekOrigin.Begin);
                            stream2.Write(buffer4, 0, buffer4.Length);
                            num2 += buffer4.Length;
                        }
                        catch (Exception)
                        {
                            flag = false;
                        }
                        finally
                        {
                            if (stream3 != null)
                            {
                                stream3.Dispose();
                            }
                        }
                        if (flag)
                        {
                            goto Label_0142;
                        }
                    }
                }
            }
        Label_0142:
            return !flag;
        }

        public static bool Encrypt(byte[] srcData, int validLength, out byte[] desData)
        {
            bool flag;
            byte[] key = new byte[0];
            byte[] iv = new byte[0];
            desData = new byte[0];
            if (!GetIVAndKey(out iv, out key))
            {
                return false;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream stream = new MemoryStream();
            try
            {
                using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(key, iv), CryptoStreamMode.Write))
                {
                    stream2.Write(srcData, 0, validLength);
                    stream2.FlushFinalBlock();
                }
                desData = stream.ToArray();
                flag = true;
            }
            catch (Exception)
            {
                desData = null;
                flag = false;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
            return flag;
        }

        public static bool EncryptFile(string srcFile, string destFile)
        {
            if (!File.Exists(srcFile))
            {
                return false;
            }
            if (!Directory.Exists(Path.GetDirectoryName(destFile)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(destFile));
            }
            byte[] key = new byte[0];
            byte[] iv = new byte[0];
            if (!GetIVAndKey(out iv, out key))
            {
                return false;
            }
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] buffer = new byte[0x400];
            byte[] buffer4 = null;
            bool flag = false;
            using (FileStream stream = new FileStream(srcFile, FileMode.Open, FileAccess.Read))
            {
                using (FileStream stream2 = new FileStream(destFile, FileMode.Create, FileAccess.Write))
                {
                    int num = 0;
                    int num2 = 0;
                    int count = 0;
                    while (num < stream.Length)
                    {
                        stream.Seek((long)num, SeekOrigin.Begin);
                        count = stream.Read(buffer, 0, buffer.Length);
                        num += count;
                        MemoryStream stream3 = new MemoryStream();
                        try
                        {
                            using (CryptoStream stream4 = new CryptoStream(stream3, provider.CreateEncryptor(key, iv), CryptoStreamMode.Write))
                            {
                                stream4.Write(buffer, 0, count);
                                stream4.FlushFinalBlock();
                            }
                            buffer4 = stream3.ToArray();
                            stream2.Seek((long)num2, SeekOrigin.Begin);
                            stream2.Write(buffer4, 0, buffer4.Length);
                            num2 += buffer4.Length;
                        }
                        catch (Exception)
                        {
                            flag = false;
                        }
                        finally
                        {
                            if (stream3 != null)
                            {
                                stream3.Dispose();
                            }
                        }
                        if (flag)
                        {
                            goto Label_0142;
                        }
                    }
                }
            }
        Label_0142:
            return !flag;
        }

        private static bool GetIVAndKey(out byte[] iv, out byte[] key)
        {
            iv = new byte[8];
            key = new byte[8];
            if (8 == s_Key.Length)
            {
                iv = Encoding.Default.GetBytes(s_IV);
                key = Encoding.Default.GetBytes(s_Key);
                return true;
            }
            if (0x10 != s_Key.Length)
            {
                return false;
            }
            for (int i = 0; i < s_Key.Length; i += 2)
            {
                byte num2 = 0;
                try
                {
                    num2 = Convert.ToByte(s_Key.Substring(i, 2), 0x10);
                }
                catch (Exception)
                {
                    return false;
                }
                iv[i / 2] = num2;
                key[i / 2] = num2;
            }
            return true;
        }

        // Properties
        public static string S_Key
        {
            get
            {
                return s_Key;
            }
            set
            {
                s_Key = value;
                s_IV = value;
            }
        }
    }
}
